package com.liwu.publish.platform.common.response;

import com.liwu.publish.platform.common.exception.PublishException;
import lombok.Data;

/**
 * @Auther: liwu
 * @Date: 2019/10/23 19:39
 * @Description:
 */
@Data
public class Response<T> {

    private int code;
    private String msg;
    private T data;


    private Response() {
        this.code = ResStatus.SUCCESS.getCode();
        this.msg = ResStatus.SUCCESS.getMsg();
    }

    private Response(T data) {
        this.code = ResStatus.SUCCESS.getCode();
        this.msg = ResStatus.SUCCESS.getMsg();
        this.data = data;
    }

    private Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Response(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Response<T> success() {
        return new Response<>();
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> error(int code, String msg) {

        return new Response<>(code, msg);
    }

    public static <T> Response<T> error(int code, String msg, T data) {

        return new Response<>(code, msg, data);
    }

    public static <T> Response<T> error(Exception e) {
        if (e instanceof PublishException) {
            return error((PublishException) e);
        }
        return new Response<>(ResStatus.UNEXPECTED_EXP.getCode(), ResStatus.UNEXPECTED_EXP.getMsg());
    }

    public static <T> Response<T> error(PublishException e) {

        return new Response<>(e.getCode(), e.getMessage());
    }

}
